package com.fh.shop.api.order.controller;

import com.fh.shop.api.annotation.Idempotency;
import com.fh.shop.api.annotation.NeedLogin;
import com.fh.shop.api.common.Constant;
import com.fh.shop.biz.IOrderCommonService;
import com.fh.shop.util.ServerResponse;
import com.fh.shop.api.member.vo.MemberVo;
import com.fh.shop.api.order.biz.IOrderService;
import com.fh.shop.api.order.param.OrderParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/orders")
@Api(tags = "订单接口")
public class OrderController {

    @Resource(name = "orderService")
    private IOrderService orderService;

    @Resource(name = "orderCommonService")
    private IOrderCommonService orderCommonService;

    @Resource
    private HttpServletRequest request;

    @Idempotency
    @PostMapping
    @NeedLogin
    @ApiOperation("增加订单")
    @ApiImplicitParam(name = "x-auth",value = "请求头",required = true,paramType = "header")
    public ServerResponse add(OrderParam orderParam){
        MemberVo memberVo = (MemberVo) request.getAttribute(Constant.MEMBERVO);
        Long memberId = memberVo.getId();
        orderParam.setMemberId(memberId);
        return orderService.add(orderParam);
    }

    @GetMapping
    @NeedLogin
    @ApiOperation("查询订单")
    @ApiImplicitParam(name = "x-auth",value = "请求头",required = true,paramType = "header")
    public ServerResponse list(){
        MemberVo memberVo = (MemberVo) request.getAttribute(Constant.MEMBERVO);
        Long memberId = memberVo.getId();
        return orderService.findOrder(memberId);
    }

    @PostMapping("/cancel/{id}")
    @NeedLogin
    @ApiOperation("取消订单")
    @ApiImplicitParams({
            @ApiImplicitParam (name = "id",value = "订单ID",readOnly = true,dataType = "java.lang.String"),
            @ApiImplicitParam(name = "x-auth",value = "请求头",required = true,paramType = "header")
            })
    public ServerResponse cancelOrder(@PathVariable("id")String orderId){
        return orderCommonService.cancelOrder(orderId);
    }
}
